﻿using System;
using RimWorld;
using Verse;
using Verse.AI;

namespace RI_Living
{
	// Token: 0x02000007 RID: 7
	public class WorkGiver_RI_Resource_SilkwormEgg : WorkGiver_Scanner
	{
		// Token: 0x1700000F RID: 15
		// (get) Token: 0x06000030 RID: 48 RVA: 0x00002B94 File Offset: 0x00000D94
		public override ThingRequest PotentialWorkThingRequest
		{
			get
			{
				return ThingRequest.ForDef(this.def.GetModExtension<Building_FermentationDefMod>().BuildingDef);
			}
		}

		// Token: 0x17000010 RID: 16
		// (get) Token: 0x06000031 RID: 49 RVA: 0x00002BBC File Offset: 0x00000DBC
		public override PathEndMode PathEndMode
		{
			get
			{
				return PathEndMode.Touch;
			}
		}

		// Token: 0x06000032 RID: 50 RVA: 0x00002BCF File Offset: 0x00000DCF
		public static void ResetStaticData()
		{
			WorkGiver_RI_Resource_SilkwormEgg.TemperatureTrans = "BadTemperature".Translate();
			WorkGiver_RI_Resource_SilkwormEgg.NoWortTrans = "NoWort".Translate();
		}

		// Token: 0x06000033 RID: 51 RVA: 0x00002BFC File Offset: 0x00000DFC
		public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
		{
			Building_Fermentation building_Fermentation = t as Building_Fermentation;
			bool flag = building_Fermentation == null || building_Fermentation.Fermented || building_Fermentation.SpaceLeftForWort <= 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				float ambientTemperature = building_Fermentation.AmbientTemperature;
				CompProperties_TemperatureRuinable compProperties = building_Fermentation.def.GetCompProperties<CompProperties_TemperatureRuinable>();
				bool flag2 = ambientTemperature < compProperties.minSafeTemperature + 2f || ambientTemperature > compProperties.maxSafeTemperature - 2f;
				if (flag2)
				{
					JobFailReason.Is(WorkGiver_RI_Resource_SilkwormEgg.TemperatureTrans, null);
					result = false;
				}
				else
				{
					bool flag3 = t.IsForbidden(pawn) || !pawn.CanReserve(t, 1, -1, null, forced);
					if (flag3)
					{
						result = false;
					}
					else
					{
						bool flag4 = pawn.Map.designationManager.DesignationOn(t, DesignationDefOf.Deconstruct) != null;
						if (flag4)
						{
							result = false;
						}
						else
						{
							bool flag5 = this.FindWort(pawn, building_Fermentation) == null;
							if (flag5)
							{
								JobFailReason.Is(WorkGiver_RI_Resource_SilkwormEgg.NoWortTrans, null);
								result = false;
							}
							else
							{
								result = !t.IsBurning();
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x06000034 RID: 52 RVA: 0x00002D0C File Offset: 0x00000F0C
		public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
		{
			Building_Fermentation barrel = (Building_Fermentation)t;
			Thing t2 = this.FindWort(pawn, barrel);
			return JobMaker.MakeJob(RI_LivingDefOf.FillRI_Building_SilkwormRearingBox, t, t2);
		}

        private Thing FindWort(Pawn pawn, Building_Fermentation barrel)
        {
            Predicate<Thing> validator = (Thing x) => !x.IsForbidden(pawn) && pawn.CanReserve(x, 1, -1, null, false);
            return GenClosest.ClosestThingReachable(pawn.Position, pawn.Map, ThingRequest.ForDef(this.def.GetModExtension<Building_FermentationDefMod>().AddThingDef), PathEndMode.ClosestTouch, TraverseParms.For(pawn, Danger.Deadly, TraverseMode.ByPawn, false, false, false, true), 9999f, validator, null, 0, -1, false, RegionType.Set_Passable, false, false);
        }

        // Token: 0x04000018 RID: 24
        private static string TemperatureTrans;

		// Token: 0x04000019 RID: 25
		private static string NoWortTrans;
	}
}
